const initEle = require('./initEle')()
const camelcase = require('./camelcase')
const computedBehavior = require('miniprogram-computed');

let utils = require('../utils/compile/utils');

module.exports = function (componentDefine, fileName, refs) {
  let component = componentDefine.default;
  if (!component.data) {
    component.data = {}
  }
  if (utils.isFunction(component.data)) {
    component.data = component.data()
  }

  if (!component.behaviors) {
    component.behaviors = []
  }
  component.behaviors = component.behaviors.concat([computedBehavior])
  component.options = Object.assign({
    addGlobalClass: true,
    multipleSlots: true
  }, component.options)
  component.componentName = camelcase(fileName)

  let relationNodes = {}

  if (!component.methods) {
    component.methods = {}
  }

  component.methods['$getPage'] = function () {
    let { com, type } = this.$getParentCom()
    if (type === 'page') {
      return com
    } else {
      return com.$getPage()
    }
  }

  component.methods['$getParent'] = function () {
    let { com, type } = this.$getParentCom()
    return com
  }

  component.methods['$getParentCom'] = function () {
    let type = 'component'
    let _com = this.selectOwnerComponent()
    if (_com.__route__) {
      type = 'page'
    }
    return {
      type,
      com: _com
    }
  }

  // component.methods.mtGetRelationNodes = function() {
  //   return relationNodes
  // }

  // let relation = component.mtRelation
  // if (relation) {
  //   let relationRet = {}
  //   for (let key in relation) {
  //     relationRet[key] = {
  //       type: relation[key].type,
  //       target: relation[key].target,
  //       linked (target) {
  //         console.d('开始嫖到 ', target.is, target.$comID)
  //         if (target.$comID) {
  //           target.$relationParent = this
  //           relationNodes[target.$comID] = target
  //         }
  //       },
  //       linkChanged(target) {
  //         // 每次有子节点被移动后执行，target是该节点实例对象，触发在该节点moved生命周期之后
  //         console.d('可恶 居然被发现了', target)
  //       },
  //       unlinked (target) {
  //         console.d('嫖到失联 ', target.is, target.$comID)
  //         if (target.$comID) {
  //           delete relationNodes[target.$comID]
  //         }
  //       }
  //     }
  //   }
  //   component.relations = Object.assign(component.relations || {}, relationRet);
  // }

  let _handles = {}
  for (let handleName in component.handles) {
    _handles[handleName] = function(e) {
      component.handles[handleName].apply(this, [e, e.currentTarget.dataset])
    }
  }

  if (!component.attached) {
    component.attached = function () {}
  }

  component.attached = initEle.initStaticRef(component.attached, refs, {
    from: 'component',
    fileName: fileName,
    onLoad(context, options = {}) {
      // console.log('context', context, context.$curPage())
      // context.$owner = context.selectOwnerComponent()
      context.$message = {
        success() {
          let p = context.$curPage()
          if (p && p.$globalMessageInstance && p.$globalMessageInstance.show) {
            p.$globalMessageInstance.show('success')
          }
        },
        error(text) {
          let p = context.$curPage()
          if (p && p.$globalMessageInstance && p.$globalMessageInstance.show) {
            p.$globalMessageInstance.show('error', {
              reason: text
            })
          }
        }
      }

    }
  })

  component.methods = {
    ...component.methods,
    ..._handles,
  }

  return component
}
